// components/list/list.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    mesinfo: Object,
    isindex: Boolean,
  },

  /**
   * 组件的初始数据
   */
  data: {
    islike: false,
    touchStartTime:'', //触摸开始时间戳
    touchEndTime:'',   //触摸结束时间戳
  },

  /**
   * 组件的方法列表
   */
  methods: {
    todetail(){
      if(this.data.touchEndTime-this.data.touchStartTime < 2000){
        wx.navigateTo({
          url: '../detail/detail?messid=' + this.properties.mesinfo._id ,
        })
      }
      //console.log(this.data.mesinfo);
    },

    todetail2(){
      if(this.data.touchEndTime-this.data.touchStartTime < 2000){
        wx.navigateTo({
          url: '../../detail/detail?messid=' + this.properties.mesinfo._id ,
        })
      }
      //console.log(this.data.mesinfo);
    },

    switchlike(){
      if(this.data.touchEndTime-this.data.touchStartTime > 2000){
        return
      }
      const db = wx.cloud.database();
      if(this.data.islike){
        this.setData({
          islike: false
        })
        let lNum = this.properties.mesinfo.likeNum - 1
        this.setData({
          'mesinfo.likeNum' : lNum
        })
        db.collection('messageInfo').doc(this.properties.mesinfo._id).update({
          data: {
            likeNum: lNum
          }
        })
      }
      else{
        this.setData({
          islike: true
        })
        let lNum = this.properties.mesinfo.likeNum + 1
        this.setData({
          'mesinfo.likeNum' : lNum
        })
        db.collection('messageInfo').doc(this.properties.mesinfo._id).update({
          data: {
            likeNum: lNum
          }
        })
      }
    },
    //记录点击开始的时间点
    mytouchstart(e) {
      console.log(e.timeStamp)
      this.setData({
        touchStartTime:e.timeStamp
      })
    },
    //记录点击结束的时间点。如果时间间隔大于2000毫秒，就触发新的事件
    mytouchend(e) {
      const db = wx.cloud.database();
      const _=db.command
      let userInfo = wx.getStorageSync('userInfo');
      let that = this
      this.setData({
        touchEndTime:e.timeStamp
      })
      if(e.timeStamp-this.data.touchStartTime> 2000){
        db.collection('favoriteInfo').where({
          uId: userInfo._id
        }).get().then(res =>{
          //第一次收藏
          if(res.data.length == 0){
            let _messages = []
            _messages.push(that.properties.mesinfo._id)
            db.collection('favoriteInfo').add({
              data: {
                uId: userInfo._id,
                messages: _messages,
              }
            })
            .then(res =>{
              wx.showToast({
                title: '收藏成功',
                duration: 2000
              })
            })
          }
          else{
            let messlist = res.data[0].messages
            for(let item of messlist){
              if(item == that.properties.mesinfo._id){
                wx.showToast({
                  title: '已经收藏过了喔',
                  duration: 2000
                })
              return
            }
            }
            db.collection('favoriteInfo').where({
              uId: userInfo._id
            }).update({
              data:{
                messages: _.push(that.properties.mesinfo._id)
              }
            })
            .then(res =>{
              wx.showToast({
                title: '收藏成功',
                duration: 2000
              })
            })
          }
        })
        .catch(err =>{
          console.log(err)
        })
      }
    },

    mytouchend2(e) {
      const db = wx.cloud.database();
      const _=db.command
      let userInfo = wx.getStorageSync('userInfo');
      let that = this
      this.setData({
        touchEndTime:e.timeStamp
      })
      if(e.timeStamp-this.data.touchStartTime> 2000){
        db.collection('favoriteInfo').where({
          uId: userInfo._id
        }).update({
          data:{
            messages: _.pull(that.properties.mesinfo._id)
          }
        })
        .then(res =>{ 
          wx.showToast({
            title: '删除成功',
          })
        })
        .catch(err => {
          console.log(err)
        })
      }
      
    },

  },
  
  options: {
    multipleSlots: true // 在组件定义时的选项中启用多slot支持
  },

})
